home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Programmer's Power Pack
/
Delphi Volume 1.iso
/
e_to_l
/
earth_ss
/
earth01.pas
< prev
Wrap
Pascal/Delphi Source File
|
1996-09-15
|
23KB
|
808 lines
{>
This program EARTH.EXE (then copied to EARTH.SCR) is a small screen
saver example. It displays a moving earth in the sky. The EARTH.EXE
program can also be run as a normal Windows application; in that case,
the earth will move in a variable size window, as determined by the
user.
The same main form, 'f_ScreenSaver', is here used to run the screen
saver in four different modes: execution (screen saver or normal
Windows application) and configuration (from Windows screen saver
installation option or normal Windows application).
When the program runs as a normal Windows application, clicking in the
main window will call the installation screen. In that latter mode,
the application's icon will change every second when minimized as an
icon.
When the screen saver is executed, a little trick is used to prepare
a surface on which we will draw the moving earth: the main window panel,
'BackPanel', is made invisible and the main window is resized to use
all the screen surface. In that case, we can then copy a earth bitmap on
the form canvas, and all other controls that are only useful when the
program is run in configuration mode are hidden.
The code contains everything you need to display the configuration
window and save the parameters to a file. These parameters are
read back at execution time.
Note that you need a $D directive in the main project source file,
such as 'EARTH.DPR'. It is in this file that the .EXE name is set to
'Earth'. It is also in the .DPR file that we have to specify
to NOT execute the program twice (Windows may start the Screen Saver
even when it is active).
To install, rebuild the EARTH application, then copy the resulting
file 'EARTH.EXE' to your Windows directory ('C:\WINDOWS', say)
under the name 'EARTH.SCR', then use the Configuration Panel (Desktop)
to select and possibly configure this new screen saver. You can also
run the normal .EXE file as a normal Windows application.
All the code is documented below, so modifying it should be very easy.
My own comments are introduced with '{>'.
This code is freeware. However, you can always send me an e.mail to let
me know that you got it. It is always nice to know that something we
contributed is actually used...! All the very best and have fun.
Jacques Lemieux
BΘluga Soft Information, MontrΘal, QuΘbec
Compuserve: 72470,1055
Internet: 72470.1055@compuserve.com
}
unit Earth01;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, ExtCtrls, StdCtrls ;
type
Tf_ScreenSaver = class(TForm)
BackPanel: TPanel;
SaveButton: TButton;
TimerEarth: TTimer;
Earth: TImage;
FastCheckbox: TCheckBox;
ShowSkyCheckBox: TCheckBox;
UseColorsCheckBox: TCheckBox;
UseMoreStarsCheckBox: TCheckBox;
TransparentCheckBox: TCheckBox;
PR1: TLabel;
PR2: TLabel;
ExitButton: TButton;
Icon1: TImage;
Icon2: TImage;
Icon3: TImage;
Icon4: TImage;
TimerIcon: TTimer;
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormCreate(Sender: TObject);
procedure TimerEarthTimer(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure SaveButtonClick(Sender: TObject);
procedure ExitButtonClick(Sender: TObject);
procedure ShowSkyCheckBoxClick(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormPaint(Sender: TObject);
procedure FormClick(Sender: TObject);
procedure StartSaver ;
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure TimerIconTimer(Sender: TObject);
private
{ Private declarations }
{> Read, Write the parameters block }
function WinDir : string ;
procedure read_config ;
procedure write_config ;
{> Setup procedure }
procedure Setup ;
{> Check consistency of checkboxes }
procedure check_checkboxes ;
{> Compute move factor }
function move_factor : integer ;
{> Stars handling stuff for execution mode }
function StarColor : TColor ;
procedure ComputeSky ;
public
{ Public declarations }
end;
var
f_ScreenSaver: Tf_ScreenSaver;
implementation
{$R *.DFM}
{> Transform all I/O errors in exceptions
}
{$I+}
{> Constants & Types }
const INIT_FILE = 'M_EARTH.DAT' ; {> Name of parameters file }
type RunMode = (rm_ScreenSaver, rm_ConfigWindows,
rm_ConfigNormal, rm_Normal) ;
config_params = record {> screen saver parameters }
fast : boolean ;
show_sky : boolean ;
use_colors : boolean ;
use_more_stars : boolean ;
transparent : boolean ;
x, y : integer ;
width, height : integer ;
end ;
{> Global variables }
var gl_params : config_params ; {> Configuration parameters }
gl_old_mouse_pos : TPoint ; {> Mouse position at beginning }
gl_mode : RunMode ; {> Current running mode }
gl_original_width, {> Original width, height as designed }
gl_original_height : integer ;
gl_icons : array [1..4] of TIcon ; {> Icons used when form minimized }
gl_next_icon : integer ; {> Next icon number to use }
procedure Tf_ScreenSaver.SaveButtonClick(Sender: TObject);
begin
{> Called from configuration panel: save parameters and go back to
execution mode as a normal application
}
write_config ;
gl_mode := rm_Normal ;
setup ;
end;
procedure Tf_ScreenSaver.ExitButtonClick(Sender: TObject);
begin
{> Called from configuration panel: exit the program
}
Close ;
end;
procedure Tf_ScreenSaver.check_checkboxes ;
begin
{> Make sure checkboxes are displayed appropriately
}
UseColorsCheckbox.visible := ShowSkyCheckbox.checked ;
UseMoreStarsCheckbox.visible := ShowSkyCheckbox.checked ;
TransparentCheckbox.visible := ShowSkyCheckbox.checked ;
end ;
procedure Tf_ScreenSaver.ShowSkyCheckBoxClick(Sender: TObject);
begin
{> Adjust other checkboxes when this one changes status from
checked to unchecked or vice-versa
}
check_checkboxes ;
end;
{> Routines to read and write the configuration parameters block
}
function Tf_ScreenSaver.WinDir : string ;
var WindowsDirectory : string [100] ;
length : integer ;
begin
length := GetWindowsDirectory (@WindowsDirectory [1], 100) ;
if length = 0 then halt ;
WindowsDirectory [0] := chr (length) ;
WinDir := WindowsDirectory + '\' ;
end ;
procedure Tf_ScreenSaver.read_config ;
var f : file of config_params ;
begin
{> Read back parameters file
}
AssignFile (f, WinDir + INIT_FILE) ;
try
Reset(f) ;
Read (f, gl_params) ;
CloseFile (f) ;
except
{> OOps, file not there or wrong record size, so use
default values for the parameters instead
}
gl_params.fast := False ;
gl_params.show_sky := True ;
gl_params.use_colors := False ;
gl_params.use_more_stars := False ;
gl_params.transparent := False ;
{> Params used in normal mode only
}
gl_params.x := 50 ;
gl_params.y := 50 ;
gl_params.width := earth.width * 2 ;
gl_params.height := earth.height * 2 ;
end ;
end ;
procedure Tf_ScreenSaver.write_config ;
var f : file of config_params ;
begin
{> Write back current parameters to file
}
if gl_mode in [rm_ConfigWindows, rm_ConfigNormal] then
begin
gl_params.fast := FastCheckbox.checked ;
gl_params.show_sky := ShowSkyCheckbox.checked ;
gl_params.use_colors := UseColorsCheckbox.checked ;
gl_params.use_more_stars := UseMoreStarsCheckbox.checked ;
gl_params.transparent := TransparentCheckbox.checked ;
end ;
if gl_mode = rm_Normal then
begin
gl_params.x := Lef